/*!
 * @file Iot.h
 * @brief DFRobot's Esp8266 iot
 * @n Header file for DFRobot's Esp8266 iot
 *
 * @copyright	[DFRobot](http://www.dfrobot.com), 2016
 * @copyright	GNU Lesser General Public License
 *
 * @author [](@dfrobot.com)
 * @version  V0.0.1
 * @date  2016-10-28
 */

#ifndef _IOT_H_
#define _IOT_H_

#include "MQTT_Client.h"
#include "MQTT.h"
#include "cJSON.h"


#define SubTopicNum 4
#define MAX_PAYLOAD_LEN 200

#ifdef ARDUINO_ESP8266_DFRobot_Education   ///< Choosing the condition, DFRobot server will be connected
//#define AIO_SERVER      "115.159.187.37"
#define AIO_SERVER      "183.230.40.39"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#elif defined ARDUINO_ESP8266_DFRobot              ///< Choosing the condition, 10086 cloud server will be connected
#define AIO_SERVER      "183.230.40.39"
#define AIO_SERVERPORT  6002
#endif


class Iot 
{
public:
#ifdef ARDUINO_ESP8266_DFRobot_Education
  Iot(Client *client, const char *server, uint16_t port, const char *user, const char *pass);  ///<  Select DFRobot server
#elif defined ARDUINO_ESP8266_DFRobot
  Iot(Client *client, const char *server, uint16_t port,const char *cid, const char *user, const char *pass); ///< Select 10086 server
#endif
  ~Iot();
  
  /*!
   *  @brief subscription
   */
  bool subscribe(const char* subtopic, int & index); 
  
  /*!
   *  @brief publish
   */
  bool configPublish(const char* pubtopic,uint8_t channel=0);
  bool writeRAW(const char* topicname, const char *data, uint8_t channel=0);
  /*!
   *  @brief connect
   */
  bool connect();
  
  /*!
   *  @brief Determine whether the connection
   */ 
  bool connected();

  /*!
   *  @brief To the server sends a String data type
   */ 
  void writeString(const char* dataID,const char* value);
  
  /*!
   *  @brief To the server sends a Int data type
   */ 
  void writeInt(const char* dataID,int value);
  
  /*!
   *  @brief To the server sends a Double data type
   */ 
  void writeDouble(const char* dataID,float value);

  /*!
   *  @brief Upload data
   */ 
  bool flush(char* topicname,uint8_t channel=0);

  /*!
   *  @brief Timeout function
   */ 
  void available(int16_t timeout=500);
  
  /*!
   *  @brief To the server response
   */ 
  bool setcallback(const char* topicname,SubscribeCallbackBufferType callb);

  /*!
   *  @brief Analytical data
   */ 
  bool parsejs(char* data);
  
  /*!
   *  @brief Get all the title of the release
   */ 
  bool readAll(char* recvmsg,char *data,int size);
  
  /*!
   *  @brief Read data type int
   */ 
  int readInt(char* key);
  
  /*!
   *  @brief Read data type string
   */ 
  String readstring(char* key);
  
  /*!
   *  @brief Read data type string
   */
  double readDouble(char* key);
   
  void reset(void);

	
public:
  char _subtopic1[30];
  char _subtopic2[30];
  char _subtopic3[30];
  char _subtopic4[30];
  char _pubtopic1[30];
  char _pubtopic2[30];
  char _pubtopic3[30];
  char _pubtopic4[30];
private:
  char* pJSON;
  cJSON* parse;
  cJSON* root;
  int _size;
  int IntVal;
  unsigned long long timestamp;
  
  char dfserver[20];
  uint16_t dfport;
  char dfuser[20];
  char dfpass[20];
  Client *dfclient;
  const char *command;
  char buf[60];
  
  MQTT_Client *Iotclient;
  MQTT_Subscribe *sensorSub1;
  MQTT_Subscribe *sensorSub2;
  MQTT_Subscribe *sensorSub3;
  MQTT_Subscribe *sensorSub4;

  
  MQTT_Publish *sensorPub1;
  MQTT_Publish *sensorPub2;
  MQTT_Publish *sensorPub3;
  MQTT_Publish *sensorPub4;
 
};

#endif